home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
- From: Dan Pop <danpop@mail.cern.ch>
- Newsgroups: comp.lang.c
- Subject: Re: Command line arg and segmentation fault
- Date: Fri, 2 Feb 1996 23:04:55 +0100
- Organization: CERN European Lab for Particle Physics
- Message-ID: <9602022204.AA19286@dxmint.cern.ch>
- References: <311273B8.25BF@soleil.acomp.usf.edu>
- X-NNTP-Posting-Host: hpl3sn03.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
- X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
-
- Chris Frenck <cfrenck@soleil.acomp.usf.edu> writes:
-
- >I am a beginning C student and have run into a bit of a problem. When compiling on GCC
- >(required) I get a segmentation fault. If I compile with MSVC++ at home it runs fine.
-
- Whoever required GCC did you a big favour. Your program is broken, but
- your Microsoft platform at home couldn't even let you know this.
-
- >Could someone please tell me what the error is?
-
- It's a typical newbie error, mentioned in this newsgroup's FAQ file.
-
- >void reverse(int count,char *argv[])
- >{
- > /* Variable Declarations */
- > char *argument;
- > int tempctr;
- > char *revphrase;
- >
- > /* Function Prototype */
- > char palin(char [], char []);
- >
- > for (tempctr=count; count > 1; count--)
- > { argument= argv[count];
- > palin(argument,revphrase);
- ^^^^^^^^^
- Huh??? You're passing to palin() an UNINITIALIZED pointer!!!
-
- > printf("%s ", revphrase);
- > tempctr++;
- > }
- >
- > return;
- >
- >}
- >
- >char palin(char *argument, char *revphrase)
- >{
- > /* Variable declarations */
- > int ctr1=0;
- > int ctr2=0;
- > /*SEGMENTATION FAULT IN FOR LOOP */
- > /* I also get a seg. fault if just */
- > /* the program name is entered and */
- > /* nothing else */
- >
- > for (ctr1 = strlen (argument)-1; ctr1 >=0; ctr1--) /*reverses*/
- > { revphrase[ctr1]= argument[ctr2];
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Here's the place where the segmentation fault occurs. The program tries
- to store a character at a random memory address (revphrase has never been
- initialized to point to a memory area belonging to your program) and this
- is causing the segmentation fault.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-